#include "GY_BasicsDataObjects.h"
#include "Graphics/GY_GraphicsReflection.h"
#include <QDebug>

GY_BasicsDataObjects::GY_BasicsDataObjects(const QString &guid,QGraphicsItem *pItem,QGraphicsItem *pParentItem)
    :GY_Object(guid),QGraphicsPathItem(pParentItem)
{
    //设置基类GY_Object的对象
    setpItem(pItem);
    //3纯数据类
    GY_Object::setUserData(GY_ENUM::ObjectType,QVariant::fromValue(3));
    //this是原版
    GY_Object::setUserData(GY_ENUM::IsCopy,QVariant::fromValue(0));
}

GY_Object *GY_BasicsDataObjects::copyItem(const QString &guid)
{
    if(getUserData(GY_ENUM::IsCopy) == 1) {
        GY_Object *tmp;
        this->copyItemSig(&tmp);
        //更新copyItem属性列表
        QMap<QString,GY_PropertyInfo>::Iterator iterProperty = m_PropertyInfoMap.begin();
        while (iterProperty != m_PropertyInfoMap.end()) {
            tmp->updateProperty(iterProperty.key(), iterProperty.value().propertyValue);
            ++iterProperty;
        }
        return tmp;
    }
    GY_Object* pObj;
    if(guid.isEmpty()) {
        pObj = (GY_Object*)GY_GraphicsReflection::getInstance()->newInstanceObjectClass(this->metaObject()->className(),this->getGuid()+"-"+QString::number(m_MyGuid));
        m_MyGuid++;
    }else {
        pObj = (GY_Object*)GY_GraphicsReflection::getInstance()->newInstanceObjectClass(this->metaObject()->className(),guid);
    }
    if(pObj == nullptr) {
        qDebug() << this->getGuid() << "isEmpty";
        return nullptr;
    }
    QMap<QString,GY_PropertyInfo>::Iterator iter = m_PropertyInfoMap.begin();
    while (iter != m_PropertyInfoMap.end()) {
        pObj->updateProperty(iter.key(), iter.value().propertyValue);
        ++iter;
    }
    connect(pObj,&GY_Object::copyItemSig,this,&GY_Object::originalVersion);
    pObj->setUserData(GY_ENUM::IsCopy, QVariant::fromValue(1));
    return pObj;
}




